package com.softer.wxzj.service.impl;

import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.common.util.StringUtils;
import com.softer.wxzj.common.util.UUIDUtil;
import com.softer.wxzj.entity.FileClassify;
import com.softer.wxzj.entity.MaintenanceObject;
import com.softer.wxzj.mapper.MaintenanceObjectMapper;
import com.softer.wxzj.service.MaintenanceObjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/7/16
 */
@Service
public class MaintenanceObjectServiceImpl implements MaintenanceObjectService {
    @Autowired
    private MaintenanceObjectMapper maintenanceObjectMapper;

    /**
     * 新增维修对象类别
     *
     * @param maintenanceObject 维修对象
     * @return 结果
     */
    @Override
    public CommonResult add(MaintenanceObject maintenanceObject) {
        String uuid = UUIDUtil.getUUID();
        maintenanceObject.setId(uuid);
        String userName = ShiroUserUtil.getUserName();
        maintenanceObject.setCreater(userName);
        maintenanceObject.setModifier(userName);
        return CommonResult.ResponseResult(maintenanceObjectMapper.insert(maintenanceObject), uuid);
    }

    /**
     * 查询维修对象
     *
     * @param id 维修对象ID
     * @return 维修对象
     */
    @Override
    public MaintenanceObject getInfo(String id) {
        return maintenanceObjectMapper.selectByPrimaryKey(id);
    }

    /**
     * 修改维修对象类别
     *
     * @param maintenanceObject 维修对象
     * @return 结果
     */
    @Override
    public CommonResult update(MaintenanceObject maintenanceObject) {
        String id = maintenanceObjectMapper.checkName(maintenanceObject);
        if (StringUtils.isNotEmpty(id) && !id.equals(maintenanceObject.getId())) {
            return CommonResult.ResponseResult(NumberEnum.ZERO.getNum(), "分类名称重复");
        }
        maintenanceObject.setModifier(ShiroUserUtil.getUserName());
        return CommonResult.ResponseResult(maintenanceObjectMapper.updateByPrimaryKey(maintenanceObject));
    }

    /**
     * 删除维修对象
     *
     * @param id 维修对象ID
     * @return 删除结果
     */
    @Override
    public CommonResult delete(String id) {
        //如果有使用，则不允许删除
        //校验维修对象中是否用到
        List<String> equipmentList = maintenanceObjectMapper.checkEquipment(id);
        if (!equipmentList.isEmpty()) {
            return CommonResult.ResponseResult(NumberEnum.ZERO.getNum(), "维修对象中存在该类别，禁止删除");
        }
        //校验维修项目中是否存在
        List<String> list = maintenanceObjectMapper.checkProject(id);
        if (!list.isEmpty()) {
            return CommonResult.ResponseResult(NumberEnum.ZERO.getNum(), "维修项目中存在该类别，禁止删除");
        }
        return CommonResult.ResponseResult(maintenanceObjectMapper.deleteByPrimaryKey(id));
    }

    /**
     * 获取公共设施设备分类的左侧三级菜单
     *
     * @return 三级菜单
     */
    @Override
    public FileClassify getMaintenanceObjectList() {
        List<FileClassify> list = maintenanceObjectMapper.getMaintenanceObjectList(String.valueOf(NumberEnum.ZERO.getNum()));
        if (list.isEmpty()) {
            return new FileClassify();
        }
        getChildren(list);
        FileClassify fileClassify = list.get(NumberEnum.ZERO.getNum());
        return fileClassify;
    }

    /**
     * 获取有效的公共设施设备分类的三级菜单
     * 用于维修资金使用的项目新建选择
     *
     * @param projectType 项目类型（1一般项目，2应急项目）
     * @return 三级菜单
     */
    @Override
    public FileClassify getEffectiveMaintenanceObjectList(String projectType) {
        String type = null;
        if (NumStrEnum.TWO.getNum().equals(projectType)) {
            type = NumStrEnum.ONE.getNum();
        }
        List<FileClassify> list = maintenanceObjectMapper.getEffectiveMaintenanceObjectList(String.valueOf(NumberEnum.ZERO.getNum()), type);
        if (list.isEmpty()) {
            return new FileClassify();
        }
        getEffectiveChildren(list, type);
        FileClassify fileClassify = list.get(NumberEnum.ZERO.getNum());
        return fileClassify;
    }

    /**
     * 循环遍历，获取子菜单
     *
     * @param list 一级菜单
     */
    private void getChildren(List<FileClassify> list) {
        //循环遍历
        for (FileClassify f : list) {
            List<FileClassify> result = maintenanceObjectMapper.getMaintenanceObjectList(f.getId());
            getChildren(result);
            if (result.isEmpty()) {
                result = null;
            }
            f.setChildren(result);
        }
    }

    /**
     * 循环遍历，获取子菜单
     *
     * @param list 一级菜单
     */
    private void getEffectiveChildren(List<FileClassify> list, String type) {
        //循环遍历
        for (FileClassify f : list) {
            List<FileClassify> result = maintenanceObjectMapper.getEffectiveMaintenanceObjectList(f.getId(), type);
            getEffectiveChildren(result, type);
            if (result.isEmpty()) {
                result = null;
            }
            f.setChildren(result);
        }
    }
}
